function Msg(msg,func) {
	layer.msg(msg,{icon:1},func);
}
function Warn(msg) {
	layer.msg(msg,{icon:0});
}
function Err(msg) {
	layer.msg(msg,{icon:2});
}
function Tips(msg,target,err) {
	var color = err ? '#ff0000' : '#3595CC';
	layer.tips(msg, target, {
		tips: [1, color],
		time: 5000
	});
}
function Ask(msg,func) {
	layer.confirm(msg, {icon:3,title:false},func);
}
function Load(close) {
	if(close){
		layer.closeAll('loading');
	} else {
		layer.load(0, {
		  shade: false,
		});
	}
}

function rslt( res , mode) {
	
	var newopen = 0;
	var mode = 1;
	Load(1);
	
	if(res.code) {
		rmsg = res.msg ? res.msg: '操作成功！';
		url = res.url ? res.url : res.data.url;
		if(mode){
			Msg(rmsg);
			goTo(url);
		} else {
			if(String(url).indexOf('http') != -1 )newopen = 1;
			var idx = layer.alert(rmsg,{title:false,icon:1,closeBtn: 1},function(){
				goTo(url,newopen);
				layer.close(idx);
			});
		}
		
	} else {
		Err(res.msg ? res.msg: '操作失败，稍候再试');
	}
}
function goTo(url,newopen) {
	if(url == -1) {
		history.back();
	} else if (url == 0) {
		return false
	}  else if (url == 1) {
		window.location.reload();
	} else if(url) {
		if(newopen){
			window.open(url);
		} else {
			window.location.href = url;
		}
		
	}
}
function checkForm(formId) {
	var check_flag = true;
	
	if(typeof formId == 'object'){
		form = formId;
	} else {
		form = $("#"+formId);
	}
	
	form.find(":input").each(function(i) {
		if ($(this).data("check")) {
			var v;
			if( $(this).prop('type') == 'checkbox' || $(this).prop('type') == 'radio'  ) {
				v = $(this).prop('checked');
			} else {
				v = $(this).val();
			}
			if (!validate(v, $(this).data("check"))) {
				
				$(this).data("msg", $(this).data("msg") ? $(this).data("msg") : $(this).prop("placeholder"));
				if ($(this).data("msg").indexOf("|")) {
					tmp = $(this).data("msg").split("|");
					msgtype = tmp[1];
					msg = tmp[0];
				}
				
				if(msgtype == 'popup') {
					LErr(tmp[0]);
				} else {
					Tips($(this).data("msg"),$(this),1);
					$(this).focus();
					$(this).closest('label').addClass('has-error');
				}
				//提示
				check_flag = false;
				
				return check_flag;
			} else {
				$(this).closest('label').replaceClass('has-error','has-success');
			}
		}
	})
	return check_flag;
}
/*提交表单*/
function sendForm(formId,check,mode) {
	
	if(typeof formId == 'object'){
		var btn = formId;
		form = btn.closest('form');
		btn.addClass('layui-disabled').data('txt',btn.text()).html("<i class='ace-icon fa fa-spinner fa-pulse'></i> 处理中...").prop('disabled',true);
		
	} else {
		form = $("#"+formId);
	}
	
	//检查
	check = check ? check : form.data('check');
	mode = mode ? mode : form.data('mode');
	if( check && !checkForm(form) ) {
		btn.removeClass('layui-disabled').text( btn.data('txt') ).prop('disabled',false);
		return false;
	}
	post_url = form.prop('action');
	if (form.data('ajax')){
//		var vars = $("#" + formId).serialize();
		var vars = new FormData( form[0] );
		Load();
		$.ajax({
			type : "POST",
			url : post_url,
			data : vars,
			contentType: false,
			processData: false,
//			dataType : "json",
			success : function(res){
				if(btn){
					btn.removeClass('layui-disabled').text( btn.data('txt') ).prop('disabled',false);
				}
				
				if(res.data && res.data.tip && !res.code) {
					var tipt = $('#'+res.data['tip']);
					Tips( res.msg, tipt );
					tipt.closest('.form-group').addClass('has-error');
				} else {
					rslt(res,mode);
				}
				
				Load(1);
			}
		});
	} else {
//		$("#" + formId).attr("action", post_url);
		if(!form.prop('method')) form.prop('method','post');
		form.submit();
	}
}


$(function(){
	
	layui.config({
	  base: '/static/layui/lay/modules/' //你存放新模块的目录，注意，不是layui的模块目录
	}); //加载入口
	
	$('body').on('click','.submit',function(){
		sendForm($(this));
	});
	
	$('body').on('keydown','form :input', function(event) {
        if (event.keyCode == "13") {
            //回车执行查询
            $(this).closest('form').find('.submit').click();
        }
    });
	
});


